package cn.schoolwow.quickdao.module.operation.flow.generateEntity.single;

import cn.schoolwow.quickdao.domain.operation.GenerateJavaSourceFileRequest;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

import java.io.File;

public class CreateEntityClassDirectoryFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        GenerateJavaSourceFileRequest generateJavaSourceFileRequest = (GenerateJavaSourceFileRequest) flowContext.checkData("generateJavaSourceFileRequest");
        String entityClassName = (String) flowContext.checkData("entityClassName");

        File targetFile = new File(generateJavaSourceFileRequest.javaSourceClassPath
                + "/" + generateJavaSourceFileRequest.packageName.replace(".","/")
                + "/" + entityClassName.replace(".","/")+".java");
        if(!targetFile.getParentFile().exists()){
            if(!targetFile.getParentFile().mkdirs()){
                throw new IllegalArgumentException("创建实体类文件夹失败!文件夹路径:"+targetFile.getParent());
            }
        }

        if(targetFile.exists()){
            if(!targetFile.delete()){
                throw new IllegalArgumentException("删除文件失败!文件路径:"+targetFile.getAbsolutePath());
            }
        }

        flowContext.putTemporaryData("targetFile", targetFile);
    }

    @Override
    public String name() {
        return "创建实体类所在目录";
    }
}
